capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

/* 
Figure B9
*/

local data "" ;
local output "" ;

foreach gp in all white black male female intel othmen phys parhigh parlow twopar singno { ;
	use "prwora_crime_compliers_`gp'.dta", clear ;
	drop if demo_all == . ;
	gen gp = "`gp'" ;
	tempfile `gp' ;
	save ``gp'' ;
} ;

use `all' ;
foreach gp in all white black male female intel othmen phys parhigh parlow twopar singno { ;
	append using ``gp'' ;
} ;
order gp ;
drop if _n <= 3 ;

keep if label == "all" ;

foreach var of varlist _all { ;
	rename `var' gp_`var' ;
} ;
rename gp_gp gp ;
drop gp_label ;

reshape long gp_, i(gp) j(char) string ;
reshape wide gp_, i(char) j(gp) string ;


gen		sort = 19 if char == "demo_male" ;
replace sort = 18 if char == "demo_female" ;

replace sort = 16 if char == "demo_black" ;
replace sort = 15 if char == "demo_white" ;

replace sort = 13 if char == "demo_parlow" ;
replace sort = 12 if char == "demo_parhigh" ;
replace sort = 11 if char == "demo_singno" ;
replace sort = 10 if char == "demo_twopar" ;

replace	sort = 8 if char == "firstage_lt6" ;
replace sort = 7 if char == "firstage_7to13" ;
replace sort = 6 if char == "firstage_gt13" ;
				
replace	sort = 4 if char == "demo_othment" ;
replace sort = 3 if char == "demo_intel" ;
replace sort = 2 if char == "demo_phys" ;

replace	sort = 0 if char == "demo_crimehigh" ;
replace sort = -1 if char == "demo_ueallhigh" ;
replace sort = -2 if char == "demo_demhigh" ;
replace sort = -3 if char == "demo_pophigh" ;

foreach var of varlist gp_* { ;
	replace `var' = `var' - 1 ;
} ;

local sex_gp1 "male" ;
local sex_gp2 "female" ;
local race_gp1 "white" ;
local race_gp2 "black" ;
local par_gp1 "parhigh" ;
local par_gp2 "parlow" ;
local fam_gp1 "twopar" ;
local fam_gp2 "singno" ;
local diag_gp1 "othmen" ;
local diag_gp2 "intel" ;
local diag_gp3 "phys" ;

local color_male "blue" ;
local color_female "pink" ;
local color_white "teal" ;
local color_black "olive" ;
local color_parhigh "purple" ;
local color_parlow "orange" ;
local color_twopar "purple" ;
local color_singno "orange" ;
local color_othmen "orange" ;
local color_intel "green" ;
local color_phys "purple" ;

local label_male "Male" ;
local label_female "Female" ;
local label_white "White" ;
local label_black "Black" ;
local label_parhigh "High par earnings" ;
local label_parlow "Low par earnings" ;
local label_twopar "Two parents" ;
local label_singno "Single or no parent" ;
local label_othmen "Other mental" ;
local label_intel "Intellectual" ;
local label_phys "Physical" ;


foreach set in sex race par { ;

twoway 
	/* dummy figure to create "dummy" yaxis(1) */
	(scatter sort gp_all if sort != . , color(none) yaxis(1))

	/* Actual figure */
	(scatter sort gp_``set'_gp1' if (char == "demo_male" | char == "demo_female") 
			& sort != . , msymbol(oh) color(`color_``set'_gp1'') yaxis(2))
	(scatter sort gp_``set'_gp1' if (char == "demo_black" | char == "demo_white") 
			& sort != . , msymbol(Th) color(`color_``set'_gp1'') yaxis(2))
	(scatter sort gp_``set'_gp1' if (char == "demo_parlow" | char == "demo_parhigh" | char == "demo_singno" | char == "demo_twopar") 
			& sort != . , msymbol(Oh) color(`color_``set'_gp1'') yaxis(2))
	(scatter sort gp_``set'_gp1' if (char == "firstage_lt6" | char == "firstage_7to13" | char == "firstage_gt13") 
			& sort != . , msymbol(Dh) color(`color_``set'_gp1'') yaxis(2))
	(scatter sort gp_``set'_gp1' if (char == "demo_phys" | char == "demo_intel" | char == "demo_othment") 
			& sort != . , msymbol(Sh) color(`color_``set'_gp1'') yaxis(2))
	(scatter sort gp_``set'_gp1' if (char == "demo_crimehigh" | char == "demo_ueallhigh" | char == "demo_demhigh" | char == "demo_pophigh") 
			& sort != . , msymbol(th) color(`color_``set'_gp1'') yaxis(2))
			
	(scatter sort gp_``set'_gp2' if (char == "demo_male" | char == "demo_female") 
			& sort != . , msymbol(o) color(`color_``set'_gp2'') yaxis(2))
	(scatter sort gp_``set'_gp2' if (char == "demo_black" | char == "demo_white") 
			& sort != . , msymbol(T) color(`color_``set'_gp2'') yaxis(2))
	(scatter sort gp_``set'_gp2' if (char == "demo_parlow" | char == "demo_parhigh" | char == "demo_singno" | char == "demo_twopar") 
			& sort != . , msymbol(O) color(`color_``set'_gp2'') yaxis(2))
	(scatter sort gp_``set'_gp2' if (char == "firstage_lt6" | char == "firstage_7to13" | char == "firstage_gt13") 
			& sort != . , msymbol(D) color(`color_``set'_gp2'') yaxis(2))
	(scatter sort gp_``set'_gp2' if (char == "demo_phys" | char == "demo_intel" | char == "demo_othment") 
			& sort != . , msymbol(S) color(`color_``set'_gp2'') yaxis(2))
	(scatter sort gp_``set'_gp2' if (char == "demo_crimehigh" | char == "demo_ueallhigh" | char == "demo_demhigh" | char == "demo_pophigh") 
			& sort != . , msymbol(t) color(`color_``set'_gp2'') yaxis(2))
	
	, 
	legend(order(2 "`label_``set'_gp1''" 8 "`label_``set'_gp2''"))
	title("Complier characteristics", size(medsmall))
	ylabel(none, axis(1))
	ylabel( 		
			19 "Male"  18 "Female"  
			16 "Black" 15 "White" 
			13 "Low par earn" 12 "High par earn" 11 "Single/no par" 10 "Two parents"
			8 "On SSI before 6" 7 "On SSI 7-13" 6 "On SSI after 13"
			4 "Other mental" 3 "Intellectual" 2 "Physical"
			0 "High crime" -1 "High unemployment" -2 "High Dem share" -3 "High pop density"
		   , 
		   angle(0) notick labsize(small) axis(1) ) 
	ylabel("", nogrid axis(2)) 
	ytitle("", axis(1)) 
	ytitle("", axis(2)) 
	xsc(r(-.7 .7))
	xlab(-.75 "0.25" -.5 "0.5" -.25 "0.75" 0 "1" .25 "1.25" .5 "1.5" .75 "1.75", axis(1)) 
	xtitle("Under- or over-representation in complier group", size(medsmall))
	graphregion(color(white))
	bgcolor(white)
	xlabel(,nogrid)
	xline(0,lcolor(gray))
	yscale(lstyle(none) axis(1))
	yscale(lstyle(none) axis(2))
	;
graph export "`output'/figB9_`set'.png", width(1000) height(750) replace ;
} ;

foreach set in diag { ;
twoway 
	/* dummy figure to create "dummy" yaxis(1) */
	(scatter sort gp_all if sort != . , color(none) yaxis(1))

	/* Actual figure */
	(scatter sort gp_``set'_gp1' if (char == "demo_male" | char == "demo_female") 
			& sort != . , msymbol(oh) color(`color_``set'_gp1'') yaxis(2))
	(scatter sort gp_``set'_gp1' if (char == "demo_black" | char == "demo_white") 
			& sort != . , msymbol(Th) color(`color_``set'_gp1'') yaxis(2))
	(scatter sort gp_``set'_gp1' if (char == "demo_parlow" | char == "demo_parhigh" | char == "demo_singno" | char == "demo_twopar") 
			& sort != . , msymbol(Oh) color(`color_``set'_gp1'') yaxis(2))
	(scatter sort gp_``set'_gp1' if (char == "firstage_lt6" | char == "firstage_7to13" | char == "firstage_gt13") 
			& sort != . , msymbol(Dh) color(`color_``set'_gp1'') yaxis(2))
	(scatter sort gp_``set'_gp1' if (char == "demo_phys" | char == "demo_intel" | char == "demo_othment") 
			& sort != . , msymbol(Sh) color(`color_``set'_gp1'') yaxis(2))
	(scatter sort gp_``set'_gp1' if (char == "demo_crimehigh" | char == "demo_ueallhigh" | char == "demo_demhigh" | char == "demo_pophigh") 
			& sort != . , msymbol(Th) color(`color_``set'_gp1'') yaxis(2))
			
	(scatter sort gp_``set'_gp2' if (char == "demo_male" | char == "demo_female") 
			& sort != . , msymbol(o) color(`color_``set'_gp2'') yaxis(2))
	(scatter sort gp_``set'_gp2' if (char == "demo_black" | char == "demo_white") 
			& sort != . , msymbol(T) color(`color_``set'_gp2'') yaxis(2))
	(scatter sort gp_``set'_gp2' if (char == "demo_parlow" | char == "demo_parhigh" | char == "demo_singno" | char == "demo_twopar") 
			& sort != . , msymbol(O) color(`color_``set'_gp2'') yaxis(2))
	(scatter sort gp_``set'_gp2' if (char == "firstage_lt6" | char == "firstage_7to13" | char == "firstage_gt13") 
			& sort != . , msymbol(D) color(`color_``set'_gp2'') yaxis(2))
	(scatter sort gp_``set'_gp2' if (char == "demo_phys" | char == "demo_intel" | char == "demo_othment") 
			& sort != . , msymbol(S) color(`color_``set'_gp2'') yaxis(2))
	(scatter sort gp_``set'_gp2' if (char == "demo_crimehigh" | char == "demo_ueallhigh" | char == "demo_demhigh" | char == "demo_pophigh") 
			& sort != . , msymbol(T) color(`color_``set'_gp2'') yaxis(2))
			
	(scatter sort gp_``set'_gp3' if (char == "demo_male" | char == "demo_female") 
			& sort != . , msymbol(o) color(`color_``set'_gp3'') yaxis(2))
	(scatter sort gp_``set'_gp3' if (char == "demo_black" | char == "demo_white") 
			& sort != . , msymbol(t) color(`color_``set'_gp3'') yaxis(2))
	(scatter sort gp_``set'_gp3' if (char == "demo_parlow" | char == "demo_parhigh" | char == "demo_singno" | char == "demo_twopar") 
			& sort != . , msymbol(o) color(`color_``set'_gp3'') yaxis(2))
	(scatter sort gp_``set'_gp3' if (char == "firstage_lt6" | char == "firstage_7to13" | char == "firstage_gt13") 
			& sort != . , msymbol(d) color(`color_``set'_gp3'') yaxis(2))
	(scatter sort gp_``set'_gp3' if (char == "demo_phys" | char == "demo_intel" | char == "demo_othment") 
			& sort != . , msymbol(s) color(`color_``set'_gp3'') yaxis(2))
	(scatter sort gp_``set'_gp3' if (char == "demo_crimehigh" | char == "demo_ueallhigh" | char == "demo_demhigh" | char == "demo_pophigh") 
			& sort != . , msymbol(t) color(`color_``set'_gp3'') yaxis(2))
	
	, 
	legend(col(3) row(1) order(2 "`label_``set'_gp1''" 8 "`label_``set'_gp2''" 14 "`label_``set'_gp3''"))
	title("Complier characteristics", size(medsmall))
	ylabel(none, axis(1))
	ylabel( 		
			19 "Male"  18 "Female"  
			16 "Black" 15 "White" 
			13 "Low par earn" 12 "High par earn" 11 "Single/no par" 10 "Two parents"
			8 "On SSI before 6" 7 "On SSI 7-13" 6 "On SSI after 13"
			4 "Other mental" 3 "Intellectual" 2 "Physical"
			0 "High crime" -1 "High unemployment" -2 "High Dem share" -3 "High pop density"
		   , 
		   angle(0) notick labsize(small) axis(1) ) 
	ylabel("", nogrid axis(2)) 
	ytitle("", axis(1)) 
	ytitle("", axis(2)) 
	xsc(r(-.7 .7))
	xlab(-.75 "0.25" -.5 "0.5" -.25 "0.75" 0 "1" .25 "1.25" .5 "1.5" .75 "1.75", axis(1)) 
	xtitle("Under- or over-representation in complier group", size(medsmall))
	graphregion(color(white))
	bgcolor(white)
	xlabel(,nogrid)
	xline(0,lcolor(gray))
	yscale(lstyle(none) axis(1))
	yscale(lstyle(none) axis(2))
	;
graph export "`output'/figB9_`set'.png", width(1000) height(750) replace ;
} ;
